
#define PICOBENCH_DEBUG
#define PICOBENCH_IMPLEMENT_WITH_MAIN

// When io processing, it takes a long time.
// If you want to test benchmark, you need to adjust the number of tests, the following Settings,

#define USER_PICOBENCH_ITERATIONS
#if defined(USER_PICOBENCH_ITERATIONS)
#define PICOBENCH_DEFAULT_ITERATIONS \
    {                                \
        8, 32, 128                   \
    }
#endif

#include "picobench.hpp"

#include <unistd.h>
#include <fcntl.h>
#include <vector>
#include <deque>
#include <cstdlib>
#include <typeinfo>
#include <unistd.h>

#define MULTITHREAD__
#ifdef MULTITHREAD__
#include <thread>
#endif

#include "../sequenceid.h"
using namespace std;
using namespace nmsp;


#define THREADS_NUM 8
#define SEQ_ID_COUNT 0xffff

void bench_sequenceid(picobench::state &s)
{
    std::cout << "sizeof(std::mutex) : " << sizeof(std::mutex) << std::endl;

    uint64_t seqNum[SEQ_ID_COUNT] = {0};
    std::atomic_uint16_t idx;
    idx.store(0);

    SequenceID::CreateInstance(2001, 3000, 30);
    SequenceID *seqId = SequenceID::GetInstance();

    auto GenSequenceID_thread_task = [&]()
    {
        seqNum[idx++] = seqId->GenSequenceID();
    };

    int curIdx = idx.load();
    std::vector<uint64_t> vec_seqNum;
    memcpy(&*vec_seqNum.begin(), seqNum, curIdx);
    auto ReleaseSequenceID_thread_task = [&]()
    {
        seqId->ReleaseSequenceID(vec_seqNum[curIdx++]);
    };


    pthread_t tids[THREADS_NUM] = {};


    

#ifdef MULTITHREAD__
    for (auto _ : s)
    {
        GenSequenceID_thread_task();
#if 0
        ReleaseSequenceID_thread_task();
#endif
    }

    // for (int i = 0; i < 10; i++)
    // {
    //     thread(thread_task).join();
    // }
#endif
}